﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Security.Principal;

using Microsoft.Office.Server.Diagnostics;

namespace Microsoft.PKS
{
	public interface ILog
	{
		void Log(string source, EventLogEntryType entryType, string msg);
	}

	public class ULSLog : ILog
	{
		#region ILog Members

		public void Log(string source, EventLogEntryType entryType, string msg)
		{
			PortalLog.LogString("Source: {0}.  {1}.  {2}", source, entryType, msg);
		}

		#endregion
	}
	public class AppEventLog : ILog
	{
		
		#region ILog Members

		public void Log(string source, EventLogEntryType entryType, string msg)
		{
			//WindowsImpersonationContext ctx = null;
			try
			{
				//Utility.UseAppPoolIdentity(ctx);
				using (EventLog e = new EventLog())
				{
					e.Source = source;
					e.WriteEntry(msg, entryType);
				}
			}
			finally
			{
				//Utility.ReturnToOriginalUser(ctx);
			}
		}
		#endregion
	}

	public class Logger
	{
		private List<ILog> logs = new List<ILog>();
		private string source;

		public Logger(string source)
		{
			this.source = source;
		}

		public void AddLog(ILog log)
		{
			logs.Add(log);
		}

		public void Write(EventLogEntryType eventType, string msg)
		{
			foreach (ILog log in logs)
			{
				log.Log(source, eventType, msg);
			}
		}

        public static Logger CreateDefaultLogger(string source)
        {
            Logger retValue = new Logger(source);

            ULSLog ulsLog = new ULSLog();
            AppEventLog appLog = new AppEventLog();

            retValue.AddLog(ulsLog);
            //retValue.AddLog(appLog);

            return retValue;
        }
	}

}


